'use strict';

angular.module('oaApp')
  .controller('ReportsCtrl', function ($scope, $http, Auth, User, Modal, $upload) {
    $scope.reports = [];
    var now = new Date();
    $scope.year = now.getFullYear();
    $scope.month = now.getMonth() + 1;
    $scope.currentUserId = Auth.getCurrentUser()._id;
    $scope.currentUser = Auth.getCurrentUser();
    $scope.$reportIndex = 0;
    $scope.progress = false;
    $scope.alertShow = false;
    $scope.nowProgress = 0;
    $scope.users = User.query();
    console.log($scope.currentUserId);
    $http.get('/api/reports?year=' + $scope.year + '&month=' + $scope.month + '&user=' +$scope.currentUserId).success(function (res) {
      $scope.reports = res;
    });

    $scope.selectDate = function () {
      console.log($scope.currentUserId);
      $http.get('/api/reports?year=' + $scope.year + '&month=' + $scope.month + '&user=' + $scope.currentUserId).success(function (res) {
        $scope.reports = res;
      });
    };

    $scope.selectProject = function (index) {
      $scope.$reportIndex = index;
    };

    $scope.delete = Modal.confirm.delReport(function(){
      $scope.selectDate();
    });


    $scope.removeFile = function(id, idx) {
      if(confirm('你确定删除该文件吗？')){
        $http.delete('/api/documents/'+id).success(function () {
          console.log('删除成功');
          $scope.reports[$scope.$reportIndex].files.splice(idx, 1);
        }).error(function () {
          console.log('删除失败');
        });
      }
    };

    $scope.upload = function () {
      $upload.upload({
        url: '/api/documents/report', // upload.php script, node.js route, or servlet url
        method: 'POST',
        headers: {'Content-Type': 'multipart/form-data'}, // only for html5
        //withCredentials: true,
        data: {reportId:$scope.reports[$scope.$reportIndex]._id,user:$scope.currentUser },
        file: $scope.reportFile // single file or a list of files. list is only for html5
        //fileName: 'doc.jpg' or ['1.jpg', '2.jpg', ...] // to modify the name of the file(s)
        //fileFormDataName: myFile, // file formData name ('Content-Disposition'), server side request form name
        // could be a list of names for multiple files (html5). Default is 'file'
        //formDataAppender: function(formData, key, val){}  // customize how data is added to the formData.
        // See #40#issuecomment-28612000 for sample code

      }).progress(function (evt) {
        $scope.progress = true;
        $scope.nowProgress = parseInt(100.0 * evt.loaded / evt.total) + '%';
        $scope.progressStatus = '上传中！';
        console.log('progress: ' + parseInt(100.0 * evt.loaded / evt.total) + '% file :'+ evt.config.file.name);
      }).success(function (data, status, headers, config) {
        // file is uploaded successfully
        $scope.progress = false;
        $scope.nowProgress = '0%';
        $scope.progressStatus = '上传成功！';
        console.log('file ' + config.file.name + 'is uploaded successfully. Response: ' + data);
        $scope.reports[$scope.$reportIndex].files.push(data);
        console.log(data);
      })
        .error(function (err) {
          console.log(err);
        });
    }

    //$scope.$watch('reportFile', function () {
    //  $scope.upload($scope.reportFile);
    //});
  })
  .controller('ReportsAddCtrl', function ($scope, $http, Auth, $state) {
    $scope.create = function () {
      $scope.submitted = true;
      var report = angular.copy($scope.report);
      report.user = Auth.getCurrentUser()._id;
      console.log(report);
      $http.post('/api/reports', report).success(function (res) {
        console.log(res);
        $state.go('app.reports');
      });

    };
  });
